package com.handsome.原型模式.浅拷贝深拷贝;

import java.util.Date;

/**
 * @author niechun
 * @date 2023/1/17 21:14
 */
public class Client {
    public static void main(String[] args) throws CloneNotSupportedException {
    /**
     *方式一 完成深拷贝
     */
        ShallowSheep shallowSheep = new ShallowSheep();
        shallowSheep.setDate(new Date());
        shallowSheep.setName("多利");
        shallowSheep.setAge(10);

        DeepSheep deepSheep = new DeepSheep();
        deepSheep.setDate(new Date());
        deepSheep.setName("多利");
        deepSheep.setAge(10);

        //浅拷贝
        ShallowSheep cloneShallow = shallowSheep.clone();
        //深拷贝
        DeepSheep cloneDeep = deepSheep.clone();
        //浅拷贝对象hashcode 相同
        System.out.println(shallowSheep.getDate() == cloneShallow.getDate());

        //深拷贝对象hashcode 不相同
        System.out.println(deepSheep.getDate() == cloneDeep.getDate());
        /**
         *方式二 完成深拷贝
         */
        ShallowSheep shallowSheep2 = new ShallowSheep();
        shallowSheep.setDate(new Date());
        shallowSheep.setName("多利");
        shallowSheep.setAge(10);

        DeepSheep2 deepSheep2 = new DeepSheep2();
        deepSheep2.setDate(new Date());
        deepSheep2.setName("阿拉");
        deepSheep2.setAge(10);

        //浅拷贝
        ShallowSheep cloneShallow2 = shallowSheep2.clone();
        //深拷贝
        DeepSheep2 cloneDeep2 = deepSheep2.clone();
        //浅拷贝对象hashcode 相同
        System.out.println(shallowSheep2.getDate() == cloneShallow2.getDate());

        //深拷贝对象hashcode 不相同
        System.out.println(deepSheep2.getDate() == cloneDeep2.getDate());

    }
}
